<?php


namespace app\shop\model\user;


use think\Model;
use think\model\concern\SoftDelete;

class UserModel extends Model
{
    use SoftDelete;

    protected $defaultSoftDelete = 0;

    protected $pk = 'user_id';
    protected $name = 'user';
    protected $autoWriteTimestamp = true;

    public function department()
    {
        return $this->hasOne(DepartmentModel::class, 'id', 'department_id')->bind(['department_name' => 'name']);
    }

    public function role()
    {
        return $this->hasOne(AdminRoleModel::class, 'id', 'role_id')->bind(['role_name']);
    }

    public function getLastLoginTimeAttr($value)
    {
        if (!$value) {
            return '';
        }
        return date('Y-m-d H:i', $value);
    }

    public function getAvatarAttr($value)
    {
        if (!$value) {
            return '';
        }
        return getDiskUrl() . $value;
    }
}